package utilities;

import java.io.File;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

public class XmlUtils {

	/**
	 * Serializes the element into a xml file.
	 * @param element the element to serialize
	 * @param filePath the target file path.
	 * @throws Exception
	 */
	public static void write(Class<?> element, String filePath) throws Exception
	{
		Serializer serializer = new Persister();
		File targetFile = new File(filePath);
		serializer.write(element, targetFile);
	}
	
	/**
	 * Deserializes the data from a xml file.
	 * @param classType 
	 * @param filePath the file path of the xml file.
	 * @return the deserialized data
	 * @throws Exception
	 */
	public static <T> T read(Class<T> classType, String filePath) throws Exception
	{
		Serializer serializer = new Persister();
		File source = new File(filePath);
		T data = serializer.read(classType, source);
		
		return data;
	}
}
